﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

namespace JamesThew.Dashboard
{

    public partial class Posts : System.Web.UI.Page
    {
        public List<Post> post;
        public PostDB pdb;
        public PostsCategoriesDB pcdb;
        public CategoryDB cdb;
        public List<Category> lc;


        protected void Page_Load(object sender, EventArgs e)
        {
            String PostType = Request.QueryString["type"];

            switch (PostType) { 
                case "announcement":
                    this.lblAddNewPost.Text = " Add New Announcement";
                    break;
                case "recipe":
                    this.lblAddNewPost.Text = " Add New Recipe";
                    break;
                case "tip":
                    this.lblAddNewPost.Text = " Add New Tip";
                    break;
                case "contest":
                    this.lblAddNewPost.Text = " Add New Contest";
                    break;
                default:
                    this.lblAddNewPost.Text = " Add New Recipe";
                    break;
            }
             

            this.pdb = new PostDB();
            this.pcdb = new PostsCategoriesDB();
            this.cdb = new CategoryDB();
            this.lc = this.cdb.Get();

            var current = HttpContext.Current;
            String submit = current.Request["ctl00$Main$btnPublish"];
            


            if (!IsPostBack)
            {
                checkboxCategories.DataSource = this.lc;
                checkboxCategories.DataValueField = "CategoryID";
                checkboxCategories.DataTextField = "CategoryName";
                checkboxCategories.DataBind();
            }

            if (Request.QueryString["action"] == "edit" && submit != "Update")
            {
                int PostID = JamesThew.Includes.Core.ParseInt(Request.QueryString["id"]);

                String[] Args = new string[]{""+PostID, null, null, null, null, null, null, null, null, null, null, null};

                try
                {
                    this.post = this.pdb.Get(Args);

                    switch (PostType)
                    {
                        case "announcement":
                            this.lblAddNewPost.Text = " Edit Announcement";
                            break;
                        case "recipe":
                            this.lblAddNewPost.Text = " Edit Recipe";
                            break;
                        case "tip":
                            this.lblAddNewPost.Text = " Edit Tip";
                            break;
                        case "contest":
                            this.lblAddNewPost.Text = " Edit Contest";
                            break;
                        default:
                            this.lblAddNewPost.Text = " Edit Recipe";
                            break;
                    }


                    this.txtPostTitle.Text = this.post[0].PostTitle;
                    this.txtPostContent.Text = this.post[0].PostContent;
                    this.txtPostExcerpt.Text = this.post[0].PostExcerpt;
                    this.txtPostThumbnail.Text = this.post[0].PostThumbnail;

                    this.hiddenDateCreated.Value = this.post[0].PostDate;

                    String ListCategory = this.pcdb.GetListCatOfPost(PostID, "number");

                    if (ListCategory.Length > 0)
                    {

                        // Assign it to an array
                        String[] ListCategoryID = ListCategory.Split(',');
                        // Loop through this array, if it value equal to DataValue Field, set it selected.

                        foreach (ListItem li in checkboxCategories.Items)
                        {
                            foreach (String s in ListCategoryID)
                            {
                                if (s == li.Value) li.Selected = true;
                            }
                        }

                    }
                }
                catch (Exception ex) {
                    Response.Redirect("404.aspx");
                }

                this.btnPublish.Text = "Update";
            }
            else { 
            
            }

        }

        public void btnPublish_Click(object sender, EventArgs e)
        {

            String PostTitle = this.txtPostTitle.Text;
            String PostContent = this.txtPostContent.Text;
            int PostAuthor = 1;
            String PostStatus = "publish";
            DateTime saveNow = DateTime.Now;
            String PostCommentStatus = "open";
            String PostPassword = "";
            int PostParent = 0;
            String PostGUID = "";
            int PostMenuOrder = 0;
            
            String PostType = Request.QueryString["type"];
            if (PostType == null || PostType.Equals("")) {
                PostType = "recipe";
            }

            String PostMimeType = "";
            int PostCommentCount = 0;
            String PostIcon = "";
            String DateCreated = "11/11/2011";
            String DateModified = "11/11/2011";
            if (Request.QueryString["action"] != "edit")
            {
                DateCreated = saveNow.ToString();
                DateModified = saveNow.ToString();
            }
            else
            {
                DateCreated = hiddenDateCreated.Value.ToString();
                DateModified = saveNow.ToString();
            }


            String PostExcerpt = this.txtPostExcerpt.Text;
            String PostThumbnail = this.txtPostThumbnail.Text;
            int PostViews = JamesThew.Includes.Core.ParseInt(this.txtPostViews.Text);

            this.pcdb = new PostsCategoriesDB();
            String SelectedCat = "";
            foreach (ListItem li in checkboxCategories.Items) {
                if (li.Selected) {
                    SelectedCat += li.Value + ',';
                }
            }

            if (SelectedCat.Length > 0 || SelectedCat != "") {
                SelectedCat = SelectedCat.TrimEnd(',');
            }

            Post post = new Post(PostAuthor, DateCreated, PostTitle, PostContent,
               PostExcerpt, PostStatus, PostCommentStatus, PostPassword,
               DateModified, PostParent, PostGUID, PostMenuOrder, PostType,
               PostMimeType, PostCommentCount, PostIcon, PostViews, PostThumbnail);

            if (Request.QueryString["action"] == "edit")
            {
                
                post.PostID = JamesThew.Includes.Core.ParseInt(Request.QueryString["id"]);

                bool res = this.pdb.Update(post);
                if (res == false)
                {
                    Response.Write("Error!");
                }
                else 
                {
                    if (SelectedCat.Length > 0 || SelectedCat != "")
                    {
                        this.pcdb.Update(post.PostID, SelectedCat);
                    }
                    Response.Write("Success");
                }
                
            }
            else
            {
                bool res = this.pdb.Add(post);
                if (res == true)
                {
                    if (SelectedCat.Length > 0 || SelectedCat != "")
                    {
                        this.pcdb.Update(post.PostID, SelectedCat);
                    }
                    Response.Write("Success");
                }
                else
                {
                    Response.Write("Error! ");
                }
            }
        }
    }
}